import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

enum PickerDateComponent { day, month, year }

// ignore: must_be_immutable
class DatePicker extends StatefulWidget {
  DateTime? upperLimit;
  DateTime? lowerLimit;

  Function dateChangedCallback;

  DatePicker({
    super.key,
    this.upperLimit,
    this.lowerLimit,
    required this.dateChangedCallback,
  });

  @override
  State<DatePicker> createState() => _DatePickerState();
}

class _DatePickerState extends State<DatePicker> {
  DateTime currentDate = DateTime.now().toLocal();
  DateTime today = DateTime.now().toLocal();

  List<String> days = [];
  List<String> months = [];
  List<String> years = [];

  @override
  void initState() {
    super.initState();

    // Initialize days, months, and years
    for (int i = 1;
        i <= DateUtils.getDaysInMonth(currentDate.year, currentDate.month);
        i++) {
      days.add(i.toString());
    }

    months = [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Aug',
      'Sep',
      'Oct',
      'Nov',
      'Dec'
    ];

    years = List.generate(
        100, (index) => (currentDate.year - 50 + index).toString());

    if (widget.upperLimit != null && currentDate.isAfter(widget.upperLimit!)) {
      currentDate = widget.upperLimit!;
    } else if (widget.lowerLimit != null &&
        currentDate.isBefore(widget.lowerLimit!)) {
      currentDate = widget.lowerLimit!;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        // Day Picker
        _buildPicker(days, PickerDateComponent.day),
        // Month Picker
        _buildPicker(months, PickerDateComponent.month),
        // Year Picker
        _buildPicker(years, PickerDateComponent.year),
      ],
    );
  }

  Widget _buildPicker(List<String> items, PickerDateComponent comp) {
    return Expanded(
      child: ListWheelScrollView.useDelegate(
        itemExtent: 50.0,
        physics: const FixedExtentScrollPhysics(),
        //perspective: RenderListWheelViewport.defaultPerspective,
        //offAxisFraction: 0.5,
        useMagnifier: true,
        magnification: 1.2,
        diameterRatio: 2.5, // 调整透视效果

        childDelegate: ListWheelChildBuilderDelegate(
          builder: (context, index) {
            return Center(
              child: Text(
                items[index % items.length],
                style: const TextStyle(fontSize: 20),
              ),
            );
          },
          childCount: items.length,
        ),
        onSelectedItemChanged: (index) {
          setState(() {
            if (comp == PickerDateComponent.day) {
              currentDate = DateTime(
                  currentDate.year, currentDate.month, int.parse(items[index]));
            } else if (comp == PickerDateComponent.month) {
              currentDate =
                  DateTime(currentDate.year, index + 1, currentDate.day);
            } else if (comp == PickerDateComponent.year) {
              currentDate = DateTime(
                  int.parse(items[index]), currentDate.month, currentDate.day);
            }
            widget.dateChangedCallback(currentDate);
          });
        },
      ),
    );
  }
}


/*

import 'package:flutter/material.dart';

class MyListWheelScrollView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScrollConfiguration(
      behavior: ScrollBehavior().copyWith(overscroll: false), // 适配不同端的滚动行为
      child: ListWheelScrollView(
        itemExtent: 60.0,
        diameterRatio: 2.5, // 调整透视效果
        physics: FixedExtentScrollPhysics(), // 使滚动更流畅
        onSelectedItemChanged: (index) {
          print("当前选中: $index");
        },
        children: List.generate(
          10,
          (index) => Container(
            alignment: Alignment.center,
            color: Colors.blueAccent,
            child: Text("Item $index", style: TextStyle(color: Colors.white)),
          ),
        ),
      ),
    );
  }
}


*/